import { _decorator, Component, find, Label, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
import { MonsterAmmo } from './monsterAmmo';
import goldManager from './managers/goldManager';
import LevelManager from './managers/LevelManager';
import { buyAtkBtn } from './buyBtns/buyAtkBtn';
import { buyHealthBtn } from './buyBtns/buyHealthBtn';
import atkUp from './managers/atkData/atkUp';
import HealthUp from './managers/healthData/healthUp';
import speedUp from './managers/speedData/speedUp';
import { buySpeedBtn } from './buyBtns/buySpeedBtn';

@ccclass('Monster')
export class Monster extends Component {

    @property()
    atk: number = 10;
    @property()
    health: number = 10;
    @property()
    gold: number = 10;


    protected onLoad(): void {
        this.moveLeft();
        this._initData();

    }
    //初始化怪物等级属性
    _initData() {
        let Levelmanager = LevelManager.getInstance();
        let level = Levelmanager.getLevel();
        this.health *= level;
        this.atk *= level;
        this.gold *= level;
    }
    start() {
    }

    //初始化怪物一出来就往左移动
    public moveLeft() {

        const myNode = this.node
        const newPosition = new Vec3(this.node.position);
        var monsterAmmo = this.node.children[0].getComponent(MonsterAmmo)

        // 使用 setPosition 方法设置节点的位置
        myNode.setPosition(newPosition);

        tween(this.node)
            .by(2.0, { position: new Vec3(-400, 0, 0) })
            .call(() => {
                monsterAmmo._init()

                globalThis.playerAmmo2._init()

            })
            .start();
    }


    monsterDie() {
        
        this.setAtkGold();
        this.viewData();

        this.node.destroy();

    }

    viewData(){
        let findCanvas = find('Canvas')

        let buyatkbtn = findCanvas.getComponent(buyAtkBtn)
        let buyhealthbtn = findCanvas.getComponent(buyHealthBtn)
        let buyspeedbtn = findCanvas.getComponent(buySpeedBtn)
        //判断是否打开购买按钮
        let atkup = atkUp.getInstance();
        atkup.getAtkbool();
        let Healthup = HealthUp.getInstance();
        Healthup.getHealthbool();
        let speedup = speedUp.getInstance();
        speedup.getSpeedBool();
        //显示所有信息
        buyhealthbtn.renderGold();
        buyatkbtn.renderGold();
        buyspeedbtn.renderGold();
    }

    setAtkGold() {
        let goldmanager = goldManager.getInstance();
        let thisgold = this.gold;
        goldmanager.killMonsterAddGold(thisgold)

        var gold = goldmanager.getGold();

        var goldView = find('Canvas/金币显示')

        let label = goldView.getComponent(Label);

        label.string = `金币:${gold}`;
    }




}
